{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading ../data/san+francisco,ca.csv...\n",
      "Loading ../data/new+york,ny.csv...\n",
      "Loading ../data/springfield,ma.csv...\n",
      "Loading ../data/boston,ma.csv...\n",
      "Loading ../data/springfield,il.csv...\n",
      "Loading ../data/albany,ny.csv...\n",
      "Loading ../data/los+angeles,ca.csv...\n",
      "Loading ../data/chicago,il.csv...\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date_time</th>\n",
       "      <th>max_temp</th>\n",
       "      <th>min_temp</th>\n",
       "      <th>city</th>\n",
       "      <th>state</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-12-11 00:00:00</td>\n",
       "      <td>13</td>\n",
       "      <td>8</td>\n",
       "      <td>San Francisco</td>\n",
       "      <td>CA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-12-11 03:00:00</td>\n",
       "      <td>13</td>\n",
       "      <td>8</td>\n",
       "      <td>San Francisco</td>\n",
       "      <td>CA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-12-11 06:00:00</td>\n",
       "      <td>13</td>\n",
       "      <td>8</td>\n",
       "      <td>San Francisco</td>\n",
       "      <td>CA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-12-11 09:00:00</td>\n",
       "      <td>13</td>\n",
       "      <td>8</td>\n",
       "      <td>San Francisco</td>\n",
       "      <td>CA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-12-11 12:00:00</td>\n",
       "      <td>13</td>\n",
       "      <td>8</td>\n",
       "      <td>San Francisco</td>\n",
       "      <td>CA</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             date_time  max_temp  min_temp           city state\n",
       "0  2018-12-11 00:00:00        13         8  San Francisco    CA\n",
       "1  2018-12-11 03:00:00        13         8  San Francisco    CA\n",
       "2  2018-12-11 06:00:00        13         8  San Francisco    CA\n",
       "3  2018-12-11 09:00:00        13         8  San Francisco    CA\n",
       "4  2018-12-11 12:00:00        13         8  San Francisco    CA"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Load min-temp and max-temp data from multiple cities\n",
    "# Turn all of that data into a single data frame with state, city, date, min, and max temp\n",
    "\n",
    "import glob\n",
    "\n",
    "all_dfs = [] \n",
    "\n",
    "for one_filename in glob.glob('../data/*,*.csv'): \n",
    "    print(f'Loading {one_filename}...')\n",
    "\n",
    "    city, state = (\n",
    "        one_filename\n",
    "        .removeprefix('../data/')\n",
    "        .removesuffix('.csv')\n",
    "        .split(',')\n",
    "    )\n",
    "\n",
    "\n",
    "    one_df = (\n",
    "        pd\n",
    "        .read_csv(one_filename,\n",
    "                  usecols=[0, 1, 2], \n",
    "                  names=['date_time',\n",
    "                         'max_temp',\n",
    "                         'min_temp'], \n",
    "                  header=0) \n",
    "        .assign(city=city.replace('+', ' ').title(), \n",
    "                state=state.upper()) \n",
    "    )\n",
    "\n",
    "    all_dfs.append(one_df) \n",
    "\n",
    "df = pd.concat(all_dfs) \n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 1\n",
    "\n",
    "Run \"describe\" on the minimum and maximum temperature for each state-city combination"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>min_temp</th>\n",
       "      <th>max_temp</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>state</th>\n",
       "      <th>city</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">CA</th>\n",
       "      <th rowspan=\"5\" valign=\"top\">Los Angeles</th>\n",
       "      <th>count</th>\n",
       "      <td>728.000000</td>\n",
       "      <td>728.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>10.637363</td>\n",
       "      <td>17.054945</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>2.705200</td>\n",
       "      <td>2.708640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>4.000000</td>\n",
       "      <td>12.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>9.000000</td>\n",
       "      <td>15.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">NY</th>\n",
       "      <th rowspan=\"5\" valign=\"top\">New York</th>\n",
       "      <th>min</th>\n",
       "      <td>-14.000000</td>\n",
       "      <td>-12.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>-4.000000</td>\n",
       "      <td>2.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>4.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>7.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>12.000000</td>\n",
       "      <td>15.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>64 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                           min_temp    max_temp\n",
       "state city                                     \n",
       "CA    Los Angeles count  728.000000  728.000000\n",
       "                  mean    10.637363   17.054945\n",
       "                  std      2.705200    2.708640\n",
       "                  min      4.000000   12.000000\n",
       "                  25%      9.000000   15.000000\n",
       "...                             ...         ...\n",
       "NY    New York    min    -14.000000  -12.000000\n",
       "                  25%     -4.000000    2.000000\n",
       "                  50%      0.000000    4.000000\n",
       "                  75%      2.000000    7.000000\n",
       "                  max     12.000000   15.000000\n",
       "\n",
       "[64 rows x 2 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Grouping by state-city combinations, get the min and max temperatures\n",
    "# Then apply the `describe` method, which returns a data frame\n",
    "df.groupby(['state', 'city'])[['min_temp', 'max_temp']].apply(DataFrame.describe)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 2\n",
    "\n",
    "Running `describe` works, but by default, we only see the first and last few rows from each result. Using `pd.set_option` to change the value of `display_max_rows`, make it possible to see all of the results in Jupyter, then reset the option to 10 rows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>min_temp</th>\n",
       "      <th>max_temp</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>state</th>\n",
       "      <th>city</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"16\" valign=\"top\">CA</th>\n",
       "      <th rowspan=\"8\" valign=\"top\">Los Angeles</th>\n",
       "      <th>count</th>\n",
       "      <td>728.000000</td>\n",
       "      <td>728.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>10.637363</td>\n",
       "      <td>17.054945</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>2.705200</td>\n",
       "      <td>2.708640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>4.000000</td>\n",
       "      <td>12.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>9.000000</td>\n",
       "      <td>15.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>11.000000</td>\n",
       "      <td>16.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>12.000000</td>\n",
       "      <td>19.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>17.000000</td>\n",
       "      <td>23.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">San Francisco</th>\n",
       "      <th>count</th>\n",
       "      <td>728.000000</td>\n",
       "      <td>728.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>8.252747</td>\n",
       "      <td>12.604396</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>2.021036</td>\n",
       "      <td>1.437399</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>3.000000</td>\n",
       "      <td>9.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>7.000000</td>\n",
       "      <td>12.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>8.000000</td>\n",
       "      <td>13.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>10.000000</td>\n",
       "      <td>14.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>13.000000</td>\n",
       "      <td>15.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"16\" valign=\"top\">IL</th>\n",
       "      <th rowspan=\"8\" valign=\"top\">Chicago</th>\n",
       "      <th>count</th>\n",
       "      <td>728.000000</td>\n",
       "      <td>728.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>-5.076923</td>\n",
       "      <td>-0.736264</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>6.255857</td>\n",
       "      <td>6.128985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>-28.000000</td>\n",
       "      <td>-25.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>-9.000000</td>\n",
       "      <td>-3.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>-4.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>-1.000000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>6.000000</td>\n",
       "      <td>9.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">Springfield</th>\n",
       "      <th>count</th>\n",
       "      <td>728.000000</td>\n",
       "      <td>728.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>-4.857143</td>\n",
       "      <td>2.076923</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>6.508184</td>\n",
       "      <td>6.273423</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>-25.000000</td>\n",
       "      <td>-20.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>-8.000000</td>\n",
       "      <td>-2.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>-5.000000</td>\n",
       "      <td>2.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>-1.000000</td>\n",
       "      <td>7.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>10.000000</td>\n",
       "      <td>16.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"16\" valign=\"top\">MA</th>\n",
       "      <th rowspan=\"8\" valign=\"top\">Boston</th>\n",
       "      <th>count</th>\n",
       "      <td>728.000000</td>\n",
       "      <td>728.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>-3.142857</td>\n",
       "      <td>2.868132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>4.957195</td>\n",
       "      <td>4.945277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>-14.000000</td>\n",
       "      <td>-12.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>-6.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>-3.000000</td>\n",
       "      <td>2.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>6.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>9.000000</td>\n",
       "      <td>17.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">Springfield</th>\n",
       "      <th>count</th>\n",
       "      <td>728.000000</td>\n",
       "      <td>728.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>-6.032967</td>\n",
       "      <td>1.472527</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>5.384681</td>\n",
       "      <td>5.266678</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>-20.000000</td>\n",
       "      <td>-16.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>-10.000000</td>\n",
       "      <td>-2.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>-6.000000</td>\n",
       "      <td>2.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>-2.000000</td>\n",
       "      <td>4.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>6.000000</td>\n",
       "      <td>15.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"16\" valign=\"top\">NY</th>\n",
       "      <th rowspan=\"8\" valign=\"top\">Albany</th>\n",
       "      <th>count</th>\n",
       "      <td>728.000000</td>\n",
       "      <td>728.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>-5.956044</td>\n",
       "      <td>0.362637</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>5.599984</td>\n",
       "      <td>5.294136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>-19.000000</td>\n",
       "      <td>-14.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>-10.000000</td>\n",
       "      <td>-3.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>-6.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>-2.000000</td>\n",
       "      <td>4.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>7.000000</td>\n",
       "      <td>13.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">New York</th>\n",
       "      <th>count</th>\n",
       "      <td>728.000000</td>\n",
       "      <td>728.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>-1.054945</td>\n",
       "      <td>4.208791</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>5.025082</td>\n",
       "      <td>4.619238</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>-14.000000</td>\n",
       "      <td>-12.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>-4.000000</td>\n",
       "      <td>2.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>4.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>7.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>12.000000</td>\n",
       "      <td>15.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                             min_temp    max_temp\n",
       "state city                                       \n",
       "CA    Los Angeles   count  728.000000  728.000000\n",
       "                    mean    10.637363   17.054945\n",
       "                    std      2.705200    2.708640\n",
       "                    min      4.000000   12.000000\n",
       "                    25%      9.000000   15.000000\n",
       "                    50%     11.000000   16.000000\n",
       "                    75%     12.000000   19.000000\n",
       "                    max     17.000000   23.000000\n",
       "      San Francisco count  728.000000  728.000000\n",
       "                    mean     8.252747   12.604396\n",
       "                    std      2.021036    1.437399\n",
       "                    min      3.000000    9.000000\n",
       "                    25%      7.000000   12.000000\n",
       "                    50%      8.000000   13.000000\n",
       "                    75%     10.000000   14.000000\n",
       "                    max     13.000000   15.000000\n",
       "IL    Chicago       count  728.000000  728.000000\n",
       "                    mean    -5.076923   -0.736264\n",
       "                    std      6.255857    6.128985\n",
       "                    min    -28.000000  -25.000000\n",
       "                    25%     -9.000000   -3.000000\n",
       "                    50%     -4.000000    0.000000\n",
       "                    75%     -1.000000    3.000000\n",
       "                    max      6.000000    9.000000\n",
       "      Springfield   count  728.000000  728.000000\n",
       "                    mean    -4.857143    2.076923\n",
       "                    std      6.508184    6.273423\n",
       "                    min    -25.000000  -20.000000\n",
       "                    25%     -8.000000   -2.000000\n",
       "                    50%     -5.000000    2.000000\n",
       "                    75%     -1.000000    7.000000\n",
       "                    max     10.000000   16.000000\n",
       "MA    Boston        count  728.000000  728.000000\n",
       "                    mean    -3.142857    2.868132\n",
       "                    std      4.957195    4.945277\n",
       "                    min    -14.000000  -12.000000\n",
       "                    25%     -6.000000    0.000000\n",
       "                    50%     -3.000000    2.000000\n",
       "                    75%      0.000000    6.000000\n",
       "                    max      9.000000   17.000000\n",
       "      Springfield   count  728.000000  728.000000\n",
       "                    mean    -6.032967    1.472527\n",
       "                    std      5.384681    5.266678\n",
       "                    min    -20.000000  -16.000000\n",
       "                    25%    -10.000000   -2.000000\n",
       "                    50%     -6.000000    2.000000\n",
       "                    75%     -2.000000    4.000000\n",
       "                    max      6.000000   15.000000\n",
       "NY    Albany        count  728.000000  728.000000\n",
       "                    mean    -5.956044    0.362637\n",
       "                    std      5.599984    5.294136\n",
       "                    min    -19.000000  -14.000000\n",
       "                    25%    -10.000000   -3.000000\n",
       "                    50%     -6.000000    0.000000\n",
       "                    75%     -2.000000    4.000000\n",
       "                    max      7.000000   13.000000\n",
       "      New York      count  728.000000  728.000000\n",
       "                    mean    -1.054945    4.208791\n",
       "                    std      5.025082    4.619238\n",
       "                    min    -14.000000  -12.000000\n",
       "                    25%     -4.000000    2.000000\n",
       "                    50%      0.000000    4.000000\n",
       "                    75%      2.000000    7.000000\n",
       "                    max     12.000000   15.000000"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.set_option('display.max_rows',1000)\n",
    "df.groupby(['state', 'city'])[['min_temp', 'max_temp']].apply(DataFrame.describe)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.set_option('display.max_rows',10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 3\n",
    "\n",
    "What is the average difference in temperature (i.e., max - min) for each of the cities in our data set?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "state  city         \n",
       "CA     Los Angeles      12.0\n",
       "       San Francisco     8.0\n",
       "IL     Chicago          34.0\n",
       "       Springfield      35.5\n",
       "MA     Boston           26.0\n",
       "       Springfield      28.5\n",
       "NY     Albany           26.5\n",
       "       New York         26.5\n",
       "dtype: float64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# We'll use lambda to calculate max-min for each value in the group, and then get the mean of those values\n",
    "df.groupby(['state', 'city'])[['min_temp', 'max_temp']].apply(lambda g: np.mean(g.max() - g.min()) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
